package mysql

import (
	"gorm.io/gorm"
	"video-manager/usersrpc/usersrpc"
)

type Video struct {
	gorm.Model
	Upload_user_id int    `gorm:"type:int(11);commit:'上传用户ID'"`
	Video_title    string `gorm:"type:varchar(50);not null;commit:'视频标题'"`
	Video_category string `gorm:"type:varchar(50);not null;commit:'视频分类'"`
	Video_link     string `gorm:"type:varchar(500);not null;commit:'视频URL'"`
	Cover_link     string `gorm:"type:varchar(500);not null;commit:'视频封面URL'"`
	Video_intro    string `gorm:"type:varchar(500);commit:'视频简介'"`
	Play_num       int    `gorm:"type:int(11);default:0;commit:'播放量'"`
	Like_num       int    `gorm:"type:int(11);default:0;commit:'点赞数'"`
	Collect_num    int    `gorm:"type:int(11);default:0;commit:'收藏数'"`
	Comment_num    int    `gorm:"type:int(11);default:0;commit:'评论数'"`
	Release_time   string `gorm:"type:varchar(300);commit:'发布时间'"`
	Video_status   int    `gorm:"type:int(11);default:1;commit:'视频状态'"`
	Topic_id       int    `gorm:"type:int(11);default:0;commit:'话题ID'"`
}

func (v *Video) UploadVideo(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *Video) FindVideoByVid(db *gorm.DB, id int64) error {
	return db.Debug().Where("videos.id= ?", id).Find(&v).Error
}

func (v *Video) ResetVideo(db *gorm.DB) error {
	return db.Debug().Updates(v).Error
}

func (v *Video) DelVideo(db *gorm.DB, id int64) error {
	return db.Debug().Where("videos.id= ?", id).Delete(v).Error
}

func (v *Video) AddPlayNum(db *gorm.DB, id int64) error {
	return db.Debug().Model(Video{}).Where("videos.id= ?", id).Update("play_num", gorm.Expr("play_num  + ?", 1)).Error
}

func (v *Video) AddVideo(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func VideoList(db *gorm.DB, list []*usersrpc.VideoList, in *usersrpc.VideoListRequest) ([]*usersrpc.VideoList, error) {
	/*
		SELECT
		  videos.id AS video_id,
		  videos.video_title AS video_title,
		  videos.video_category AS video_category,
		  videos.video_link AS video_link,
		  videos.cover_link AS cover_link,
		  videos.video_intro AS video_intro,
		  videos.play_num AS play_num,
		  videos.like_num AS like_num,
		  videos.collect_num AS collect_num,
		  videos.comment_num AS comment_num,
		  videos.release_time,
		  videos.video_status AS video_status,
		  users.nick_name AS nick_name,
		  users.avatar_link AS avatar_link,
		  topics.topic_name AS topic_name
		FROM
		  videos
		  JOIN users ON videos.upload_user_id = users.id
		  JOIN topics ON videos.topic_id = topics.id
	*/
	tx := db.Debug().Model(Video{}).Scopes(Paginate(int(in.Page), int(in.Size))).Select(
		"videos.id AS video_id",
		"videos.video_title AS video_title",
		"videos.video_category AS video_category",
		"videos.video_link AS video_link",
		"videos.cover_link AS cover_link",
		"videos.video_intro AS video_intro",
		"videos.play_num AS play_num",
		"videos.like_num AS like_num",
		"videos.collect_num AS collect_num",
		"videos.comment_num AS comment_num",
		"videos.created_at as created_at",
		"videos.video_status AS video_status",
		"users.nick_name AS nick_name",
		"users.avatar_link AS avatar_link",
		"topics.topic_name AS topic_name").
		Joins("JOIN users ON videos.upload_user_id = users.id").
		Joins("JOIN topics ON videos.topic_id = topics.id")
	if in.VideoTypes == 1 {
		tx.Order("created_at DESC")
	}
	if in.VideoTypes == 2 {
		tx.Order("play_num DESC")
	}
	err = tx.Find(&list).Error
	return list, err
}
func VideoShow(db *gorm.DB, id int64) usersrpc.VideoShowResponse {
	var list usersrpc.VideoShowResponse
	db.Debug().Model(Video{}).Select(
		"videos.id AS video_id",
		"videos.video_title AS video_title",
		"videos.video_category AS video_category",
		"videos.video_link AS video_link",
		"videos.cover_link AS cover_link",
		"videos.video_intro AS video_intro",
		"videos.play_num AS play_num",
		"videos.like_num AS like_num",
		"videos.collect_num AS collect_num",
		"videos.comment_num AS comment_num",
		"videos.created_at as created_at",
		"videos.video_status AS video_status",
		"users.nick_name AS nick_name",
		"users.avatar_link AS avatar_link",
		"topics.topic_name AS topic_name").
		Joins("JOIN users ON videos.upload_user_id = users.id").
		Joins("JOIN topics ON videos.topic_id = topics.id").
		Where("videos.id= ?", id).
		First(&list)
	return list
}
